//
//  ST_BTLaunchController.swift
//  BTVideo
//
//  Created by DouLei on 2019/7/8.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import NIMSDK
import SwiftyUserDefaults
import RealmSwift
import SwifterSwift
import SwiftyJSON
import RxCocoa
import RxSwift


enum ST_TIAN_TAB:Int{
    case recommend = 0
    case room
    case square
    case msg
    case myCenter
}

class ST_BTLaunchController: UIViewController {
    
    
    static let shared = ST_BTLaunchController()
    
    @objc static func sharedInstance()->ST_BTLaunchController{
        return shared
    }
    
    //    MARK: - Life cycle
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        print("内存释放问题----❌收到 警告了")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        MobClick.event("Page_view", attributes: ["Page_name":"HomePage"])
        
        view.addSubview(controllers_container_view)
        controllers_container_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        //      个人中心页面
        my_center_controller.view.isHidden = true
        
        //      消息中心页面
        message_controller.view.isHidden = true
        
        //       default display 推荐页
        home_controller.view.isHidden = false
        
        view.addSubview(tabbar_back_view)
        tabbar_back_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(tabbar_back_corner)
            make.height.equalTo(BTSafeAreaBottom + 49 + tabbar_back_corner)
        }
        
        setup_tabs()
        
        supplement_other_widget()
        
        binding()
    }
    
    /// 广告页
    private func loadAdvert(){
        
        if let advertModel = ST_AdvertManger.shared.get_advert(), Defaults[DefaultsKeys.BTLoginSuccess] , let display = ST_DTBootManager.shared.displayAdvert, display == true {
            
            let ad = ST_BTAdvertViewController()
            ad.outside_model = advertModel
            self.addChild(ad)
            self.view.addSubview(ad.view)
            ad.view.snp.makeConstraints { (make) in
                make.edges.equalToSuperview()
            }
            
            advert_controller = ad
            
            advertStatus.onNext(.displaying)
        }else{
            advertStatus.onNext(.finished)
        }
        
        
    }
    
    private func supplement_other_widget()  {
        
        ST_LiveBeautyWidget.shared.layout_live_beauty_behavior.accept(self.view)
        
        ST_LaunchChargeWidget.shared.layout_charge_behavior.accept(self.view)
        
        ST_PushPeculiarityCoordinator.shared.layout_behavior.accept(self.view)
        
        ST_PushDailyCoordinator.shared.layout_behavior.accept(self.view)
        
        quick_chat_dispatcher.start(root_view: view)
        
        loadAdvert()
        
    }
    
    
    private func binding(){
        
        
        ST_ConversationAdapter.shared.unread_number_observer.distinctUntilChanged().subscribe(onNext: {[weak self] _ in
            
            self?.setting_unread_msg_count()
            
        }).disposed(by: disposeBag)
      
        NotificationCenter.default.rx.notification(NSNotification.Name.my_avatar_updated).subscribe(onNext: {[weak self] _ in
            
            self?.update_mycenter_tab_avatar()
            
        }).disposed(by: disposeBag)
        
        
    }
    
    var disposeBag = DisposeBag()
    
    private let quick_chat_dispatcher = ST_QuickChatFloatableControlDispatcher()
    
    fileprivate weak var advert_controller:ST_BTAdvertViewController?
    
    enum AdvertStatusEnum{
        case none
        case displaying
        case finished
    }
    
    var advertStatus = BehaviorSubject<AdvertStatusEnum>(value: .none)
    
    fileprivate var setup_tabbar_back_view_border = false
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        if self.view.frame.width == ScreenWidth && !setup_tabbar_back_view_border {
            
            tabbar_back_view.borderColor = Color.init(hexString: "#EFF0F2")
            tabbar_back_view.borderWidth = 1
            
            tabbar_back_view.roundCorners([UIRectCorner.topLeft,UIRectCorner.topRight], radius: 30)
            
            setup_tabbar_back_view_border = true
        }
    }
    
//    MARK: - private method
    
    fileprivate func female_tips_value()->Bool{
        
        if Defaults[.isPanda]{
            return false
        }
        
        guard let user = ST_BTUserInfoManager.shared.current_account_info?.userinfo, user.sex == .female     else { return false }
        
        let key = "femaleTips_" + user.accid + "temp0123"
        
        let lastValue = Defaults.bool(forKey: key)
        
        return !lastValue
        
    }
    
    fileprivate func set_due_female_tips_value(){
        
        guard let user = ST_BTUserInfoManager.shared.current_account_info?.userinfo, user.sex == .female     else { return  }
        
        let key = "femaleTips_" + user.accid + "temp0123"
        
        Defaults.setValue(true, forKey: key)
        
    }
    
    
    //    MARK: - Properties
    
    //
    fileprivate lazy var launch_toast_new_msg = false
    fileprivate lazy var toast_new_msg_image_view:UIImageView = {
        let control = UIImageView(image: ST_ImageUtils.imageName("toastNewMsg"))
        control.alpha = 0
        return control
    }()
    
    
    fileprivate var tabbar_back_corner = 30.cgFloat
    
    fileprivate lazy var tabbar_back_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        
        control.borderColor = Color.init(hexString: "#EFF0F2")
        control.borderWidth = 1
        control.cornerRadius = 30
        
        return control
    }()
    
    
    fileprivate var tabbarBaseTag = 1027
    
    //    fileprivate lazy var whiteToolBackView:UIView = {
    //        let control = UIView()
    //        control.backgroundColor = Color.white
    //        control.isHidden = true
    //        return control
    //    }()
    
    //    fileprivate lazy var toolLineView:UIView = {
    //        let control = UIView()
    //        control.backgroundColor = Color.init(hex: 0xF2F4F7)
    //        return control
    //    }()
    
    //
    //    fileprivate let homeBtnWDSelectedImage = ST_ImageUtils.imageName("首页_icon_首页WD")!
    let tab_bar_selected_attri: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
    
    
    let tabBarAttri: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.medium),.foregroundColor: UIColor.init(hexString: "#ACB4C0")!]
    
    
    fileprivate lazy var tabbar_image_edge =  UIEdgeInsets.init(top: 3, left: (49-28)/2.0, bottom: 18, right: (49-28)/2.0)
    
    fileprivate lazy var homeBtn:ST_VerticalButton = {
        
        let control = ST_VerticalButton()
        //        control.backgroundColor = .random
        
        control.image_view_edges = self.tabbar_image_edge
        control.setImage(ST_ImageUtils.imageName("tabbar_icon_0_sel")!, for: UIControl.State.selected)
        control.setImage(ST_ImageUtils.imageName("tabbar_icon_0")!, for: UIControl.State.normal)
        control.tag = tabbarBaseTag
        control.add_touch_up(insideTarget: self, action: #selector(tabbar_click(sender:)))
        control.isSelected = true
        
        let attrString = NSMutableAttributedString(string: "缘分")
        attrString.addAttributes(tabBarAttri, range: NSRange(location: 0, length: attrString.length))
        control.setTitleForAllStates(attrString)
        
        let selectedAttrString = NSMutableAttributedString(string: "缘分")
        selectedAttrString.addAttributes(tab_bar_selected_attri, range: NSRange(location: 0, length: selectedAttrString.length))
        control.setTitle(selectedAttrString, for: UIControl.State.selected)
        
        
        return control
    }()
    
    
    fileprivate lazy var slide_number_label:UILabel = {
        let control = UILabel()
        control.cornerRadius = 18/2.0
        control.backgroundColor = UIColor.init(hexString: "#EB3C3C")!
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.isHidden = true
        return control
    }()
    
    
    fileprivate lazy var room_tab_btn:ST_VerticalButton = {
        let control = ST_VerticalButton()
        control.image_view_edges = self.tabbar_image_edge
        control.setImage(ST_ImageUtils.imageName("tabbar_icon_1_sel")!, for: UIControl.State.selected)
        control.setImage(ST_ImageUtils.imageName("tabbar_icon_1")!, for: UIControl.State.normal)
        control.tag = tabbarBaseTag + 1
        control.add_touch_up(insideTarget: self, action: #selector(tabbar_click(sender:)))
        let attrString = NSMutableAttributedString(string: "房间")
        attrString.addAttributes(tabBarAttri, range: NSRange(location: 0, length: attrString.length))
        control.setTitleForAllStates(attrString)
        
        let selectedAttrString = NSMutableAttributedString(string: "房间")
        selectedAttrString.addAttributes(tab_bar_selected_attri, range: NSRange(location: 0, length: selectedAttrString.length))
        control.setTitle(selectedAttrString, for: UIControl.State.selected)
        
        return control
        
    }()
    
    fileprivate lazy var peculiarity_tab_btn:ST_VerticalButton = {
        let control = ST_VerticalButton()
        
        control.image_view_edges = self.tabbar_image_edge
        
        control.setImage(ST_ImageUtils.imageName("tabbar_icon_2_sel")!, for: UIControl.State.selected)
        control.setImage(ST_ImageUtils.imageName("tabbar_icon_2")!, for: UIControl.State.normal)
        
        control.tag = tabbarBaseTag + 2
        
        control.add_touch_up(insideTarget: self, action: #selector(tabbar_click(sender:)))
        
        
        let attrString = NSMutableAttributedString(string: "广场")
        attrString.addAttributes(tabBarAttri, range: NSRange(location: 0, length: attrString.length))
        control.setTitleForAllStates(attrString)
        
        let selectedAttrString = NSMutableAttributedString(string: "广场")
        selectedAttrString.addAttributes(tab_bar_selected_attri, range: NSRange(location: 0, length: selectedAttrString.length))
        control.setTitle(selectedAttrString, for: UIControl.State.selected)
        
        return control
        
    }()
    
    fileprivate lazy var msg_btn:ST_VerticalButton = {
        
        let control = ST_VerticalButton()
        control.image_view_edges = self.tabbar_image_edge
        control.setImage(ST_ImageUtils.imageName("首页_icon_消息")!, for: UIControl.State.normal)
        control.setImage(ST_ImageUtils.imageName("首页_icon_消息sel")!, for: UIControl.State.selected)
        control.tag = tabbarBaseTag + 3
        control.add_touch_up(insideTarget: self, action: #selector(tabbar_click(sender:)))
        
        
        let attrString = NSMutableAttributedString(string: "消息")
        attrString.addAttributes(tabBarAttri, range: NSRange(location: 0, length: attrString.length))
        control.setTitleForAllStates(attrString)
        
        
        let selectedAttrString = NSMutableAttributedString(string: "消息")
        selectedAttrString.addAttributes(tab_bar_selected_attri, range: NSRange(location: 0, length: selectedAttrString.length))
        control.setTitle(selectedAttrString, for: UIControl.State.selected)
        
        
        control.addSubview(unread_message_label)
        unread_message_label.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.top.equalToSuperview().offset(5)
            make.height.equalTo(18)
            make.width.equalTo(18)
        }
        
        return control
    }()
    
    fileprivate lazy var unread_message_label:UILabel = {
        let control = UILabel()
        control.cornerRadius = 18/2.0
        control.backgroundColor = UIColor.init(hexString: "#EB3C3C")!
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.isHidden = true
        return control
    }()
    
    fileprivate let corner_tips_width = 10.cgFloat
    
    fileprivate lazy var pec_corner_corner_view:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hexString: "#EB3C3C")!
        control.cornerRadius = corner_tips_width/2.0
        return control
    }()
    
    fileprivate lazy var daily_corner_corner_view:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hexString: "#EB3C3C")!
        control.cornerRadius = corner_tips_width/2.0
        return control
    }()
    
    
    fileprivate lazy var mycenter_btn:ST_BTTabBarButton = {
        
        let control = ST_BTTabBarButton()
        
        if let url = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.avatar.url{
            control.setImageForAllStates(url)
        }
        
        let w = 26.cgFloat
        
        control.image_view_edges = UIEdgeInsets.init(top: 5, left: (49 - w)/2.0, bottom: 18, right: (49 - w)/2.0)
        control.imageViewCornerRadius = 13
        //        control.setImage(ST_ImageUtils.imageName("首页_icon_我的sel"), for: UIControl.State.selected)
        control.tag = tabbarBaseTag + 4
        control.add_touch_up(insideTarget: self, action: #selector(tabbar_click(sender:)))
        
        let attrString = NSMutableAttributedString(string: "我的")
        attrString.addAttributes(tabBarAttri, range: NSRange(location: 0, length: attrString.length))
        control.setTitleForAllStates(attrString)
        
        let selectedAttrString = NSMutableAttributedString(string: "我的")
        selectedAttrString.addAttributes(tab_bar_selected_attri, range: NSRange(location: 0, length: selectedAttrString.length))
        control.setTitle(selectedAttrString, for: UIControl.State.selected)
        
        return control
    }()
    
    fileprivate lazy var last_selected_tab = ST_TIAN_TAB.recommend
    
    
    fileprivate var display_same_city_on_home = false
    
    
    //    MARK: - Event
    @objc func toast_chat_to_sary(){
        
        /*
         
         if female_tips_value() {
         
         main_thread_execute(afterDelay: 0.25) {
         
         let vc = ST_ChatForSryAlertController()
         
         vc.overCurrentContext = true
         
         GlobalPresent(toController: vc, animated: false)
         }
         
         set_due_female_tips_value()
         }
         */
    }
    
    
    @objc func update_mycenter_tab_avatar(){
        
        
        if let url = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.avatar.url{
            mycenter_btn.setImageForAllStates(url)
            
            ST_SwiftConst.shared.save_my_avatar_image(url: url)
            
        }
        
        NotificationCenter.default.post(name: Notification.Name.avatarUpdated, object: nil)
        
    }
    
   
    @objc func tabbar_click(sender:ST_VerticalButton){
        
        if sender.isSelected{
            return
        }
       
        let currentIdx = sender.tag - tabbarBaseTag
        
        guard let curr = ST_TIAN_TAB(rawValue: currentIdx) else{
            return
        }
        
        quick_chat_dispatcher.isHidden = curr == .msg
        
        ST_BTGlobalDataCenter.shared.clickMainTab(tag: currentIdx)
        
        ST_LiveBeautyWidget.shared.change_widget_state(tabControlTag:currentIdx)
        
        ST_LaunchChargeWidget.shared.change_widget_state(tabControlTag:currentIdx)
     
        setSelected(value: false, tab: last_selected_tab)
        
        setSelected(value: true, tab: curr)
        
        last_selected_tab = curr
        
        
        switch curr {
        case .msg:
            ST_ChatModuleViewController.shared.disply_current_tab_again()
            if toast_new_msg_image_view.superview != nil{ toast_new_msg_image_view.alpha = 0 }
        case .room:
            pec_corner_corner_view.isHidden = true
//            if Defaults[.talkAlert281] == false && Defaults[.isPanda] == false{
//                self.toastDirectedTalk()
//            }
        case .square:
            daily_corner_corner_view.isHidden = true
        case .myCenter:
            
            /*
            -点击table“我的”时，判断优先级：
            1.未认证，先弹去认证
            2.认证成功后，弹去添加特质
            3.添加特质后，弹去录制语音
            4.录制语音后，弹去上传视频封面
            */
            
            if FACE_AVATAE_STATE == 1 {
                
                main_thread_execute(afterDelay: 0.25) {
                    
                    ToastDetectFaceTips {
                        BTVerticalAlertViewController.createTipsForDetectedAlert()
                    }
                    
                }
            }else if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .female , let attributes =  ST_BTUserInfoManager.shared.additional_attributes,attributes.voiceSignStatus == 0{
                
                BTVerticalAlertViewController.presentVoiceSignTips()
                
            }else if let attributes =  ST_BTUserInfoManager.shared.additional_attributes,attributes.canUploadRoomCover  && attributes.currentRoomCover == nil{
                
                main_thread_execute(afterDelay: 0.25) {
                    
                    let alert = BTVerticalAlertViewController.createTipsForRoomCoverAlert()
                    
                    alert.confirm_closure = {
                        
                        self.navigationController?.pushViewController(ST_EditMyInfoViewController())
                        
                    }
                    
                    
                    GlobalPresent(toController: alert, animated: false)
                }
            }
            
            main_thread_execute(afterDelay: 2) {
                SCComponets.alert_dispatcher?.request_award_sync()
            }
            
            ST_EngagementCentServer.shared.push_log()
            
        default:
            
            break
        }
        
        
    }
    
    fileprivate func setSelected(value:Bool = true,tab:ST_TIAN_TAB){
        
        let item = fetch_button_with_controller(tab: tab)
        
        item.vc.view.isHidden = !value
        item.tabBtn.isSelected = value
        
    }
    
    
    fileprivate func fetch_button_with_controller(tab:ST_TIAN_TAB)->(tabBtn:UIControl,vc:UIViewController){
        
        switch tab {
        case .recommend:
            return (tabBtn:homeBtn,vc:home_controller)
        case .room:
            return (tabBtn:room_tab_btn,vc:live_module_controller)
        case .square:
            return (tabBtn:peculiarity_tab_btn,vc:pec_controller)
        case .msg:
            return (tabBtn:msg_btn,vc:message_controller)
        case .myCenter:
            return (tabBtn:mycenter_btn,vc:my_center_controller)
        }
    }
    
    //    @objc func recordClick(sender:UIButton){
    //
    //
    //        PresentMakeVideo()
    //
    //    }
    
    //    MARK: - Controller Properties
    
    fileprivate(set) lazy var home_controller:ST_PersonalFeedController = {
        let vc = ST_PersonalFeedController()
        self.addChild(vc)
        controllers_container_view.addSubview(vc.view)
        vc.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        vc.view.isHidden = true
        return vc
    }()
    
    
    fileprivate lazy var pec_controller:ST_PeculiarityModuleViewController = {
        let vc = ST_PeculiarityModuleViewController()
        self.addChild(vc)
        controllers_container_view.addSubview(vc.view)
        vc.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        vc.view.isHidden = true
        return vc
    }()
    
    //    fileprivate lazy var loveController = ST_LoveModuleController()
    
    fileprivate lazy var live_module_controller:ST_LiveP2PViewController = {
        let vc = ST_LiveP2PViewController()
        self.addChild(vc)
        controllers_container_view.addSubview(vc.view)
        vc.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        vc.view.isHidden = true
        return vc
    }()
    
    
    fileprivate lazy var message_controller : ST_ChatModuleViewController = {
        let vc = ST_ChatModuleViewController.shared
        self.addChild(vc)
        controllers_container_view.addSubview(vc.view)
        vc.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        vc.view.isHidden = true
        return vc
    }()
    
    fileprivate lazy var my_center_controller : ST_AIMycenterViewController  = {
        let vc = ST_AIMycenterViewController.shared
        self.addChild(vc)
        controllers_container_view.addSubview(vc.view)
        vc.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        vc.view.isHidden = true
        return vc
    }()
    
    fileprivate lazy var copid_view:ST_CupidsArrowView = {
        let control = ST_CupidsArrowView()
        return control
    }()
    
    fileprivate lazy var controllers_container_view:UIView = {
        let control = UIView()
        return control
    }()
    
   
    
    //    MARK: - public Method
    @objc fileprivate func setup_tabs(){
        
        tabbar_back_view.removeSubviews()
       
            
            let leftAssistView = UIView()
            
            let rightAssistView = UIView()
            
            [homeBtn,leftAssistView,peculiarity_tab_btn,rightAssistView,mycenter_btn].forEach({ tabbar_back_view.addSubview($0) })
        

//        home_controller.layout_others_widget()
        
//        my_center_controller.refresh_header_layout()
        
//        ST_BTMessageController.shared.reloadHeaderView()
        
        
        
        switch tabbar_back_view.subviews.count {
        case 5:
            
            guard let item0 = tabbar_back_view.subviews[safe: 0],let item1 = tabbar_back_view.subviews[safe: 1],let item2 = tabbar_back_view.subviews[safe: 2],let item3 = tabbar_back_view.subviews[safe: 3],let item4 = tabbar_back_view.subviews[safe: 4] else { return  }
            
            let offset = 22-(49-28)/2.0
            
            item2.snp.makeConstraints { (make) in
                make.width.height.equalTo(49)
                make.top.centerX.equalToSuperview()
            }
            
            item0.snp.makeConstraints { (make) in
                make.width.height.top.equalTo(item2)
                make.left.equalToSuperview().offset(offset)
            }
            
            item4.snp.makeConstraints { (make) in
                make.width.height.top.equalTo(item2)
                make.right.equalToSuperview().offset(-offset)
            }
            
            item1.snp.makeConstraints { (make) in
                make.left.equalTo(item0.snp.right)
                make.right.equalTo(item2.snp.left)
                make.top.bottom.equalToSuperview()
            }
            
            item1.addSubview(room_tab_btn)
            room_tab_btn.snp.makeConstraints { (make) in
                make.width.height.equalTo(item2)
                make.centerX.top.equalToSuperview()
            }
            
            item3.snp.makeConstraints { (make) in
                make.left.equalTo(item2.snp.right)
                make.right.equalTo(item4.snp.left)
                make.top.bottom.equalToSuperview()
            }
            
            item3.addSubview(msg_btn)
            msg_btn.snp.makeConstraints { (make) in
                make.width.height.equalTo(item2)
                make.centerX.top.equalToSuperview()
            }
            
            room_tab_btn.addSubview(pec_corner_corner_view)
            pec_corner_corner_view.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-2)
                make.top.equalToSuperview().offset(4)
                make.width.height.equalTo(corner_tips_width)
            }
            
            peculiarity_tab_btn.addSubview(daily_corner_corner_view)
            daily_corner_corner_view.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-2)
                make.top.equalToSuperview().offset(4)
                make.width.height.equalTo(corner_tips_width)
            }
            
            break
            
        case 3:
            
            guard let item0 = tabbar_back_view.subviews[safe: 0],let item1 = tabbar_back_view.subviews[safe: 1],let item2 = tabbar_back_view.subviews[safe: 2] else { return  }
            
            let offset = 22-(49-28)/2.0
            
            
            item1.snp.makeConstraints { (make) in
                make.width.height.equalTo(49)
                make.top.centerX.equalToSuperview()
            }
            
            item0.snp.makeConstraints { (make) in
                make.width.height.top.equalTo(item1)
                make.left.equalToSuperview().offset(offset)
            }
            
            item2.snp.makeConstraints { (make) in
                make.width.height.top.equalTo(item1)
                make.right.equalToSuperview().offset(-offset)
            }
            
            break
        default:
            break
        }
        
        toast_new_msg_image_view.removeFromSuperview()
        
        if let adView = advert_controller?.view{
            view.insertSubview(toast_new_msg_image_view, belowSubview: adView)
        }else{
            view.addSubview(toast_new_msg_image_view)
        }
        
        toast_new_msg_image_view.snp.makeConstraints { (make) in
            make.width.equalTo(130)
            make.height.equalTo(42)
            make.centerX.equalTo(msg_btn)
            make.bottom.equalToSuperview().offset(-54-BTSafeAreaBottom)
        }

        
        
        
        
    }
    
    func receive_copid_handle(json:JSON){
        
        if copid_view.superview == nil{
            
            self.view.addSubview(copid_view)
            
            copid_view.snp.makeConstraints { (make) in
                make.top.equalToSuperview().offset(statusBarHeight-8)
                make.size.equalTo(CGSize.init(width: ST_CupidsArrowView.width, height: ST_CupidsArrowView.height))
                make.centerX.equalToSuperview()
            }
            
        }
        copid_view.isHidden = false
        
        copid_view.json = json
        
    }
    
    func clear_unread_msg() {
        
        ClearUnreadOfMeetYout()
        
//        ST_ChatModuleViewController.shared.unreadMatchArray.removeAll()
        
        setting_unread_msg_count()
        
    }
    
    func setting_unread_msg_count(){
        
        var totalCount = 0
        if let count = try? ST_ConversationAdapter.shared.unread_number_observer.value() {
            totalCount = count
        }
        
        UIApplication.shared.applicationIconBadgeNumber = totalCount
        
        if unread_message_label.superview == nil{
            return
        }
        
        unread_message_label.isHidden = false
        
        if !launch_toast_new_msg && totalCount > 0{
            toast_new_msg_image_view.alpha = 1
            launch_toast_new_msg = true
        }else{
            toast_new_msg_image_view.alpha = 0
        }
        
        if totalCount > 99{
            unread_message_label.text = "99+"
            unread_message_label.snp.updateConstraints { (make) in
                make.width.equalTo(30)
            }
        }else if totalCount > 0 {
            unread_message_label.text = totalCount.string
            unread_message_label.snp.updateConstraints { (make) in
                make.width.equalTo(totalCount > 9 ? 24 : 18)
            }
        }else{
            unread_message_label.isHidden = true
        }
        
        
        
    }
    
    // 卡片滑动次数
    var AI_CARD_SIDE_NUMBER = 0
    
   
    
    
    
    func scroll_to_page(tab:ST_TIAN_TAB){
        
        guard let btn = fetch_button_with_controller(tab: tab).tabBtn as? ST_VerticalButton else { return  }
        
        tabbar_click(sender: btn)
        
    }
    
    
    
    @objc func current_display_controller() -> UIViewController?  {
        
        return fetch_button_with_controller(tab: last_selected_tab).vc
        
    }
}


